
#pragma once

#include "RefCnt.h"
#include <stdio.h>

class FGC
{
public:
    FGC() : mImpl(new Impl()) {
        printf("FGC construct.\n");
    }

    virtual ~FGC() {
        printf("~FGC. mImpl = %p\n", mImpl);
        if (mImpl)
            mImpl->unref();
    }

    FGC(const FGC* o) : mImpl(0) {
        printf("FGC copy* construct.\n");
        mImpl = o->mImpl;
        if (mImpl)
            mImpl->ref();
        delete o;
    }

    FGC(const FGC& o) : mImpl(0) {
        printf("FGC copy& construct.\n");
        mImpl = o.mImpl;
        if (mImpl)
            mImpl->ref();
    }

    FGC& operator=(const FGC& o) {
        printf("FGC operator=.\n");
        if (mImpl)
            mImpl->unref();
        mImpl = o.mImpl;
        if (mImpl)
            mImpl->ref();
        return *this;
    }

    class Impl : public RefCnt
    {
    public:
        virtual ~Impl() {
            printf("Impl destruct.\n");
        }
        void foo() {
            printf("Impl::foo, this = %p\n", this);
        }
    };

    Impl* operator->() { 
        printf("operator->, this = %p, mImpl = %p\n", this, mImpl);
        return mImpl; 
    }

private:
    Impl* mImpl;
};



